package com.cn.onetrip.untility;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.baidu.platform.comapi.map.v;
import com.cn.onetrip.adapter.RouteItemAdapter;
import com.cn.onetrip.objects.AdvertsObj;
import com.cn.onetrip.objects.ArticleListObj;
import com.cn.onetrip.objects.RouteObj;
import com.cn.onetrip.objects.VisitsListObj;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.HTTPUrl;
import com.cn.onetrip.untility.NetworkManager;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.upload;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;
import com.cn.onetrip.view.ContentScrollView;
import com.cn.onetrip.view.SlideMenuLayout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class TitleScrollManager {

	private final int UPDATE_VIEW_POS = 0;
	
	private int viewTop = 0;
	
	private RelativeLayout layoutTitle;
	private ContentScrollView scrollView;
	private ListView listView;
	private AdView adView;
	
	private Timer titleAnimTimer = null;
	private boolean isTitleUp = false;
	
	private int scrollY = 0;
	private boolean animStart = false;

	public TitleScrollManager(RelativeLayout layoutTitle, AdView adView,
			ContentScrollView scrollView, ListView listView) {
		
		this.layoutTitle = layoutTitle;
		this.adView = adView;
		this.scrollView = scrollView;
		this.listView = listView;

		if (scrollView != null)
			viewScrollListener();
		else if (listView != null)
			listViewScrollListener();
	}
	
	private void viewScrollListener() {
		scrollY = scrollView.getScrollY();
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			private int lastY = 0;
			private int touchEventId = -9983761;
			Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what == touchEventId) {
						if (lastY == scrollView.getScrollY()) {
							scrollY = scrollView.getScrollY();
							adView.stopAdPlay();
						} else {
							handler.sendMessageDelayed(
									handler.obtainMessage(touchEventId), 1);
							lastY = scrollView.getScrollY();
						}
					}
				}
			};
		  	   
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				switch (event.getAction()) {
				
				case MotionEvent.ACTION_UP: {
					int curScrollY = scrollView.getScrollY();
					
					if (scrollY > curScrollY && !animStart && isTitleUp) {
						titleAnimation(false);
						Log.i("titleAnimation", "titleAnimation Down");
					} else if (scrollY < curScrollY && !animStart && !isTitleUp) {
						titleAnimation(true);
						Log.i("titleAnimation", "titleAnimation Up");
					}
					
					handler.sendMessageDelayed(handler.obtainMessage(touchEventId), 1);   
				}
					break;

				default:
					break;
				}

				return false;
			}
		});
	}
	
	private void listViewScrollListener() {
		scrollY = listView.getFirstVisiblePosition();
		listView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
				int curScrollY = listView.getFirstVisiblePosition();
				
				if (scrollY > curScrollY && !animStart && isTitleUp) {
					titleAnimation(false);
					Log.i("titleAnimation", "titleAnimation Down");
				} else if (scrollY < curScrollY && !animStart && !isTitleUp) {
					titleAnimation(true);
					Log.i("titleAnimation", "titleAnimation Up");
				}
				
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					scrollY = listView.getFirstVisiblePosition();
					adView.stopAdPlay();
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
	}
	
	private Handler titleAnimHandler = new Handler(){
		public void dispatchMessage(android.os.Message msg) {

			switch (msg.what) {
				
			case UPDATE_VIEW_POS: {
				int top = (Integer)(msg.obj);
				RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, layoutTitle.getHeight());
				lp.setMargins(0, top, 0, 0);
				layoutTitle.setLayoutParams(lp);
			}	
				break;

			default:
				break;
			}
		};
	};

	private void titleAnimation(final boolean isUp) {
		animStart = true;
		
		viewTop = isUp ? 0 : -layoutTitle.getHeight();
	
		titleAnimTimer = new Timer();
		TimerTask task = new TimerTask() {
			public void run() {
				if ((viewTop < -layoutTitle.getHeight() && isUp) ||
						(viewTop > 0 && !isUp)) {
					stopTimer();
					animStart = false;
					isTitleUp = isUp;
					Log.i("titleAnimation", "titleAnimation cancle");
					return;
				}
				
				Message msg = new Message();
				msg.what = UPDATE_VIEW_POS;
				msg.obj = viewTop;
				titleAnimHandler.sendMessage(msg);
				
				if (isUp)
					viewTop -= 5;
				else 
					viewTop += 5;
				
				Log.i("titleAnimation", "titleAnimation: "+String.valueOf(viewTop));
			}
		};

		titleAnimTimer.schedule(task, 10, 10);
		
	}
	
	public void stopTimer() {
		if (titleAnimTimer != null)
			titleAnimTimer.cancel();
		
		titleAnimTimer = null;
	}
}
